﻿@{
    ViewBag.Title = "管理员";
}

@section SideNav{@Html.Partial("SideNavPartialView")}
<ol class="breadcrumb">
    <li><span class="glyphicon glyphicon-home"></span>  @Html.ActionLink("首页", "Index", "Home")</li>
    <li class="active">@Html.ActionLink("管理员", "Index", "Admin")</li>
</ol>
<div class="form-inline">
    <input id="search" type="text" name="QueryString" class="form-control" placeholder="输入检索词" />
    <input type="button" id="btnSearch" value="检索" class="btn btn-default" />
</div>
<div id="toolbar" class="btn-group" role="group">
    <button id="btn_add" class="btn btn-default"><span class="glyphicon glyphicon-plus"></span> 添加</button>
    <button id="btn_resetpassword" class="btn btn-default"><span class="glyphicon glyphicon-lock"></span> 重置密码</button>
    <button id="btn_del" class="btn btn-default"><span class="glyphicon glyphicon-remove"></span> 删除</button>
</div>
<table id="admingrid"></table>
<script type="text/javascript">
    $('#btnSearch').click(function () {
        var $table = $('#admingrid');
        $table.bootstrapTable("refresh");
    });
    $(document).ready(function () {
        //表格
        var $table = $('#admingrid');
        $table.bootstrapTable({
            toolbar: "#toolbar",
            showRefresh: true,
            showColumns: true,
            showFooter: true,
            method: "post",
            pageNumber: 1,
            pageSize: 10,
            pagination: true,
            sidePagination: "server",
            pageList: "[5, 10, 20, 50, 100]",
            url: "@Url.Action("PageList")",
            contentType: "application/x-www-form-urlencoded",
            queryParams: queryParams,
            queryParamsType: "limit", //参数格式,发送标准的RESTFul类型的参数请求
            columns: [
                { title: "state", checkbox: true },
                { title: "ID", field: "AdminID" },                    
                { title: "帐号", field: "Accounts" },
                { title: "登录时间", field: "LoginTime", formatter: function (value) { return moment(value).format("YYYY-MM-DD HH:mm:ss") } },
                { title: "登录IP", field: "LoginIP" },
                { title: "创建时间", field: "CreateTime", formatter: function (value) { return moment(value).format("YYYY-MM-DD HH:mm:ss") } },
                {
                    title: "操作", field: "AdminID", formatter: function (value) {
                        return "<a class='btn btn-sm btn-primary' href='#' data-method='Edit' data-value=" + value + ">编辑</a>"
                    }
                }
            ],
            onAll: function (name, args) {
                if ((name == "load-success.bs.table") || (name == "column-swtich.bs.table")) {
                    //修改
                    $("a[data-method='Edit']").click(function () {
                        var id = $(this).attr("data-value");
                        var editDialog = new BootstrapDialog({
                            title: "<span class='glyphicon glyphicon-edit'></span> 编辑角色",
                            message: function (dialog) {
                                var $message = $('<div></div>');
                                var pageToLoad = dialog.getData('pageToLoad');
                                $message.load(pageToLoad);
                                return $message;
                            },
                            data: { 'pageToLoad': '@Url.Action("EditPartialView")/' + id },
                            buttons: [{
                                icon: "glyphicon glyphicon-plus",
                                label: "保存",
                                action: function (dialogItself) {
                                    $.post($("form").attr("action"), $("form").serializeArray(),
                                    function (data) {
                                        if (data.Code == 1) {
                                            BootstrapDialog.show({
                                                message: data.Message,
                                                buttons: [{
                                                    icon: "glyphicon glyphicon-ok",
                                                    label: "确定",
                                                    action: function (dialogItself) {
                                                        $table.bootstrapTable("refresh");
                                                        dialogItself.close();
                                                        editDialog.close();
                                                    }
                                                }]
                                            });
                                        }
                                        else BootstrapDialog.alert(data.Message);
                                    }, "json");
                                    $("form").validate();
                                }
                            }, {
                                icon: "glyphicon glyphicon-remove",
                                label: "关闭",
                                action: function (dialogItself) {
                                    dialogItself.close();
                                }
                            }]
                        });
                        editDialog.open();
                    });
                    //修改结束
                }
            }
        });
        //表格结束
            
        //添加按钮
        $("#btn_add").click(function () {
            var addDialog = new BootstrapDialog({
                title: "<span class='glyphicon glyphicon-plus'></span>添加管理员",
                message: function (dialog) {
                    var $message = $('<div></div>');
                    var pageToLoad = dialog.getData('pageToLoad');
                    $message.load(pageToLoad);

                    return $message;
                },
                data: {
                    'pageToLoad': '@Url.Action("AddPartialView")'
                },
                buttons: [{
                    icon: "glyphicon glyphicon-plus",
                    label: "添加",
                    action: function (dialogItself) {
                        $.post($("form").attr("action"), $("form").serializeArray(), function (data) {
                            if (data.Code == 1) {
                                BootstrapDialog.show({
                                    message: data.Message,
                                    buttons: [{
                                        icon: "glyphicon glyphicon-ok",
                                        label: "确定",
                                        action: function (dialogItself) {
                                            $table.bootstrapTable("refresh");
                                            dialogItself.close();
                                            addDialog.close();
                                        }
                                    }]

                                });
                            }
                            else BootstrapDialog.alert(data.Message);
                        }, "json");
                        $("form").validate();
                    }
                }, {
                    icon: "glyphicon glyphicon-remove",
                    label: "关闭",
                    action: function (dialogItself) {
                        dialogItself.close();
                    }
                }]
            });
            addDialog.open();
        });
        //添加按钮结束


        //重置密码按钮
        $("#btn_resetpassword").click(function () {
            var selected = $table.bootstrapTable('getSelections');
            if ($(selected).length > 0) {
                BootstrapDialog.confirm("确定重置选中的" + $(selected).length + "位管理员的密码", function (result) {
                    if (result) {
                        var ids = new Array($(selected).length);
                        $.each(selected, function (index, value) {
                            ids[index] = value.AdminID;
                        });
                        $.post("@Url.Action("ResetPasswordBatch", "Admin")", { ids: ids }, function (data) {
                            if (data.Code != 0) {
                                BootstrapDialog.show({
                                    message: data.Message,
                                    buttons: [{
                                        icon: "glyphicon glyphicon-ok",
                                        label: "确定",
                                        action: function (dialogItself) {
                                            //$table.bootstrapTable("refresh");
                                            dialogItself.close();
                                        }
                                    }]

                                });
                            }
                            else BootstrapDialog.alert(data.Message);

                        }, "json");
                    }
                });
            }
            else BootstrapDialog.warning("请选择要重置密码的管理员");
        });
        //重置密码按钮结束

        //删除按钮
        $("#btn_del").click(function () {
            var selected = $table.bootstrapTable('getSelections');
            if ($(selected).length > 0) {
                BootstrapDialog.confirm("确定删除选中的" + $(selected).length + "位管理员", function (result) {
                    if (result) {
                        var ids = new Array($(selected).length);
                        $.each(selected, function (index, value) {
                            ids[index] = value.AdminID;
                        });
                        $.post("@Url.Action("DeleteJsonBatch", "Admin")", { ids: ids }, function (data) {
                            if (data.Code != 0) {
                                BootstrapDialog.show({
                                    message: data.Message,
                                    buttons: [{
                                        icon: "glyphicon glyphicon-ok",
                                        label: "确定",
                                        action: function (dialogItself) {
                                            $table.bootstrapTable("refresh");
                                            dialogItself.close();
                                        }
                                    }]

                                });
                            }
                            else BootstrapDialog.alert(data.Message);

                        }, "json");
                    }
                });
            }
            else BootstrapDialog.warning("请选择要删除的管理员");
        });
        //删除按钮结束

        //重置密码方法
        function ResetPassword(id, accounts) {
            BootstrapDialog.confirm("确定重置" + accounts + "的密码", function (result) {
                if (result) {
                    $.post("@Url.Action("ResetPassword", "Admin")", { id: id }, function (data) {
                        BootstrapDialog.alert(data.Message);
                    }, "json");
                }
            });
        };
        //重置密码结束
    });

    function queryParams(params) {
        var temp = {
            pageNumber: params.offset,
            pageSize: params.limit,
            name: document.getElementById("search").value
        };
        return temp;
    }
</script>